import scrapy
import time
from scrapy import Selector
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import logging
from selespider.items import SpiderdemoItem

logger = logging.getLogger()

class SpideraSpider(scrapy.Spider):
    name = "spidera"
    allowed_domains = ["souche.com"]
    start_urls = ["https://weidian.souche.com/#/buy?QueryOrgAllCar=false&salerUserId=ACCNBA1ycbQlq9mu&salerphone=18336352223&shareId=k76FH2z6lu&sharechannel=lj&shopCode=04435083"]    
  
    def __init__(self):
        super(SpideraSpider, self).__init__(name='spidera')
        option = ChromeOptions()
        mobileEmulation = {'deviceName':'Galaxy S5'} # 模拟手机浏览器
        # mobile_emulation = {"deviceMetrics": {"width": 375, "height": 667, "pixelRatio": 2.0},"userAgent": "Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30"}
        option.add_experimental_option('mobileEmulation',mobileEmulation)
        option.headless = True
        self.driver = webdriver.Chrome(options=option)
    
    def parse(self, response):
        # sel = Selector(response)
        # dds = sel.xpath('//*[@id="buyPage"]/div/div[6]/div[2]/div[1]/div[1]/div/div[2]/div[1]').extract()
        # logger.error('ret==========================================: {}'.format(dds))
        items = []
        for itm in response.xpath('//*[@id="buyPage"]/div/div[6]/div[2]/div[1]/div'):
            obj = SpiderdemoItem()
            imgSrc = 'https:' + itm.xpath('div/div[1]/img/@src').extract_first().split(',')[0]
            title = itm.xpath('div/div[2]/div[1]/text()').extract_first()
            onlineDte = itm.xpath('div/div[2]/div[2]/span[1]/text()').extract_first()
            distence = itm.xpath('div/div[2]/div[2]/span[2]/text()').extract_first()
            stand = itm.xpath('div/div[2]/div[2]/span[3]/text()').extract_first()

            onlinePay = itm.xpath('div/div[2]/div[3]/div[1]/text()').extract_first() + '万'
            buyPay = itm.xpath('div/div[2]/div[3]/div[3]/text()').extract_first()
            
            obj['title'] = title
            obj['imgSrc'] = imgSrc
            obj['onlineDte'] = onlineDte
            obj['distence'] = distence
            obj['stand'] = stand
            obj['onlinePay'] = onlinePay
            obj['buyPay'] = buyPay
            items.append(obj)
        logger.warning('>>>>>> item: {}'.format(items))
        logger.warning('>>>>>> item.length: {}'.format(len(items)))